package com.suzka.mapper;

import com.suzka.pojo.DrugCharge;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;
/**
 * 药品收费数据访问接口
 * 使用MyBatis注解方式实现数据库操作
 */
@Mapper
public interface DrugChargeMapper {
    @Select("SELECT dc.*, c.name as customerName, d.name as drugName " +
            "FROM drug_charge dc " +
            "LEFT JOIN customer c ON dc.customer_id = c.id " +
            "LEFT JOIN drug d ON dc.drug_id = d.id")
    List<DrugCharge> findAll();
    
    @Select("SELECT dc.*, c.name as customerName, d.name as drugName " +
            "FROM drug_charge dc " +
            "LEFT JOIN customer c ON dc.customer_id = c.id " +
            "LEFT JOIN drug d ON dc.drug_id = d.id " +
            "WHERE dc.id = #{id}")
    DrugCharge findById(Integer id);
    
    @Insert("INSERT INTO drug_charge(customer_id, drug_id, quantity, total_price, charge_date) " +
            "VALUES(#{customerId}, #{drugId}, #{quantity}, #{totalPrice}, #{chargeDate})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(DrugCharge drugCharge);
    
    @Update("UPDATE drug_charge SET customer_id=#{customerId}, drug_id=#{drugId}, " +
            "quantity=#{quantity}, total_price=#{totalPrice}, charge_date=#{chargeDate} " +
            "WHERE id=#{id}")
    int update(DrugCharge drugCharge);
    
    @Delete("DELETE FROM drug_charge WHERE id = #{id}")
    int deleteById(Integer id);
    
    @Select("SELECT dc.*, c.name as customerName, d.name as drugName " +
            "FROM drug_charge dc " +
            "LEFT JOIN customer c ON dc.customer_id = c.id " +
            "LEFT JOIN drug d ON dc.drug_id = d.id " +
            "WHERE dc.customer_id = #{customerId}")
    List<DrugCharge> findByCustomerId(Integer customerId);
    
    @Select("SELECT dc.*, c.name as customerName, d.name as drugName " +
            "FROM drug_charge dc " +
            "LEFT JOIN customer c ON dc.customer_id = c.id " +
            "LEFT JOIN drug d ON dc.drug_id = d.id " +
            "WHERE dc.drug_id = #{drugId}")
    List<DrugCharge> findByDrugId(Integer drugId);
    
    @Select("SELECT dc.*, c.name as customerName, d.name as drugName " +
            "FROM drug_charge dc " +
            "LEFT JOIN customer c ON dc.customer_id = c.id " +
            "LEFT JOIN drug d ON dc.drug_id = d.id " +
            "WHERE dc.charge_date BETWEEN #{startDate} AND #{endDate}")
    List<DrugCharge> findByDateRange(LocalDateTime startDate, LocalDateTime endDate);

}
